// import Fly from 'flyio/dist/npm/wx';
//
// import config from '../config'
//
// const fly =  new Fly()
// //配置请求基地址
// fly.config.baseURL = config.baseUrl
// fly.config.withCredentials = true;
// fly.config.timeout = 10000;
//
//
// //添加请求拦截器
// fly.interceptors.request.use((request) => {
//     //给所有请求添加自定义header
//     request.headers["X-Tag"] = "flyio";
//     let token = Taro.getStorageSync('token');
//     if (token ){
//         request.headers.authentication = token;
//     }
//     //打印出请求体
//     console.log(request.body)
//     //终止请求
//     //var err=new Error("xxx")
//     //err.request=request
//     //return Promise.reject(new Error(""))
//
//     //可以显式返回request, 也可以不返回，没有返回值时拦截器中默认返回request
//     return request;
// });
//
// //添加响应拦截器，响应拦截器会在then/catch处理之前执行
// fly.interceptors.response.use(
//     (response) => {
//         //只将请求结果的data字段返回
//         let result = response.data;
//
//         if (result && result.code === 200) {
//
//             return result.data
//         } else {
//
//             return Promise.reject(new Error(result.msg));
//         }
//     },
//     (error) => {
//         console.error(error)
//         return Promise.reject(new Error("网络错误"));
//         //发生网络错误后会走到这里
//         //return Promise.resolve("ssss")
//     }
// );
//
// export default fly;
//
//
import Taro from '@tarojs/taro'
import config from '../config'
import {HTTP_STATUS} from '../const/status'

// import { logError } from '../utils'

const token = Taro.getStorageSync("token")

export default {
    baseOptions(params, method = 'GET') {
        let {url, data} = params
        // let token = getApp().globalData.token
        // if (!token) login()
        console.log('params', params)
        let contentType = 'application/x-www-form-urlencoded'
        contentType = params.contentType || contentType

        const option = {
            isShowLoading: false,
            loadingText: '正在加载',
            url: (url.indexOf('https://') !== -1 || url.indexOf('http://') !== -1) ? url: config.baseUrl + url,
            data: data,
            method: method,
            header: {'content-type': contentType, 'authentication': token},
        }
        return new Promise((resolve, reject) => {
            Taro.request(option).then(res => {
                if (res.statusCode === HTTP_STATUS.NOT_FOUND) {
                    reject(new Error("请求资源不存在"))
                } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
                    reject(new Error("服务端出现了问题"))
                } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) {
                    reject(new Error("没有权限访问"))
                } else if (res.statusCode === HTTP_STATUS.SUCCESS) {
                    let result = res.data;
                    if (result.code === 200) {
                        resolve(result.data)
                    } else if (result.code > 200 && result.code < 400) {
                        console.log(result);
                        reject(result.data);
                    } else {
                        reject(new Error(result.msg))
                    }
                }
            }).catch(error => {
                Taro.showToast({
                    title: "网络错误",
                    icon: "none"
                })
                reject(new Error(error.message))
            })
        })

    },
    get(url, data = '') {
        let option = {url, data}
        return this.baseOptions(option)
    },
    post: function (url, data, contentType) {
        let params = {url, data, contentType}
        return this.baseOptions(params, 'POST')
    },
    delete(url, data = '') {
        let option = {url, data}
        return this.baseOptions(option,"DELETE")
    },
    put: function (url, data, contentType) {
        let params = {url, data, contentType}
        return this.baseOptions(params, 'PUT')
    }
}